<?php
namespace PsiFramework\Plugins\Emailing\Classes\Model;

use PsiFramework\Classes\Util\Object\EMail;

use PsiFramework\Applications\DefaultApp\Classes\Model\User;
use PsiFramework\Applications\DefaultApp\Classes\Model\Page;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe de campagne d'e-mail
 *
 * @package Model
 */
class EmailCampaign
{
    /**
     * @var string
     */
    protected /*string*/ $_sender;
    
    /**
     * @var Page
     */
    protected /*Page*/ $_message;
    
    /**
     * @var array
     */
    protected /*User[]*/ $_recipients;
    
    /**
     * @var array
     */
    protected /*EmailVariable[]*/ $_variables;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        
    }
    
    /**
     * @return User
     */
    public function getSender()
    {
        return $this->_sender;
    }
    
    /**
     * @return Page
     */
    public function getMessage()
    {
        return $this->_message;
    }
    
    /**
     * @return array
     */
    public function getRecipients()
    {
        return $this->_recipients;
    }
    
    /**
     * @return array
     */
    public function getVariables()
    {
        return $this->_variables;
    }
    
    /**
     * @param string $sender
     */
    public function setSender(/*string*/ $sender)
    {
        $this->_sender = $sender;
    }
    
    /**
     * @param Page $message
     */
    public function setMessage(/*Page*/ $message)
    {
        $this->_message = $message;
    }
    
    /**
     * @param array $recipients
     */
    public function setRecipients(/*array*/ $recipients)
    {
        $this->_recipients = $recipients;
    }
    
    /**
     * @param array $variables
     */
    public function setVariables(/*array*/ $variables)
    {
        $this->_variables = $variables;
    }
    
    /**
     * @param User $recipient
     */
    public function addRecipient(/*User*/ $recipient)
    {
        $this->_recipients[] = $recipient;
    }
    
    /**
     * @param EmailVariable $variable
     */
    public function addVariable(/*EmailVariable*/ $variable)
    {
        $this->_variables[] = $variable;
    }
    
    /**
     * 
     * @param User $user
     * 
     * @return Email
     */
    public function getEmailForRecipient(/*User*/ $user)
    {
        
        $message = $this->_message->getContent();
        $title = $this->_message->getTitle();
        
        foreach ($this->_variables as $variable) {
            /* @var $variable EmailVariable */
            
            $realValue = $variable->getRealValue($user);
            
            $message = str_replace('{'.$variable->getKey().'}', $realValue, $message);
            $title = str_replace('{'.$variable->getKey().'}', $realValue, $title);
        }

        $recipientString = $user->getFormattedEmailString();
        
        $email = null;
        if (!isNullOrEmptyString($recipientString)) {
            $email = new EMail($this->_sender, $recipientString, $title, $message);
        } else {
            throw new \Exception('Recipient email string is not valid!');
        }
        
        return $email;
    }
}
